package com.zdltech.lightappupdatelib.core;

import com.zdltech.lightappupdatelib.LightUpdateManager;
import com.zdltech.lightappupdatelib.entity.AppVertionEntity;
import com.zdltech.lightappupdatelib.utils.LogUtils;

import org.json.JSONObject;

import java.util.Date;

/**
 * 版本转换
 *
 * @ Des
 * @ Author jason
 */

public class AppVersionConvert {
    /**
     * 解析json,更新对象
     *
     * @param json 服务器返回的json
     * @return AppVertionEntity
     */
    public static AppVertionEntity parseJson(String json) {
        AppVertionEntity updateAppBean = new AppVertionEntity();
        try {
            JSONObject jsonObject = new JSONObject(json);
            updateAppBean.setAppName(jsonObject.optString("update"));
            updateAppBean.setAppStatus(jsonObject.optInt("appStatus"));
            updateAppBean.setAppUnique(jsonObject.optString("appUnique"));
            updateAppBean.setCreateDate(new Date(jsonObject.optLong("createDate")));
            updateAppBean.setExt(jsonObject.optString("ext"));
            updateAppBean.setFileMd5(jsonObject.optString("fileMd5"));
            updateAppBean.setFileSize(jsonObject.optLong("fileSize"));
            updateAppBean.setId(jsonObject.optString("id"));
            updateAppBean.setLastForce(jsonObject.optInt("lastForce"));
            updateAppBean.setNeedUpdate(jsonObject.optInt("needUpdate"));
            updateAppBean.setPlatform(jsonObject.optString("platform"));
            updateAppBean.setUpdateDate(new Date(jsonObject.optLong("updateDate")));
            updateAppBean.setUpdateInfo(jsonObject.optString("updateInfo"));
            updateAppBean.setUpdateType(jsonObject.optString("updateType"));
            updateAppBean.setUpdateUrl(jsonObject.optString("updateUrl"));
            updateAppBean.setVersion(jsonObject.optLong("version"));
            updateAppBean.setVersionName(jsonObject.optString("versionName"));
            updateAppBean.setRemark(jsonObject.optString("remark"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return updateAppBean;
    }


    /**
     * 用户转换自定义（自己）的更新信息到APPVertio对象
     * @param callback
     * @return
     */
    public static AppVertionEntity parseOhter(String result,AppVersionConvertCallback callback){
        return callback.convert(result);
    }
}
